package com.bayss.web.controller;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.coupon.Coupon;
import com.bayss.core.service.coupon.CouponService;
import com.bayss.web.uresponse.UserResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class CouponController extends ApiBaseController {

    @Autowired
    private CouponService couponService;

    /**
     * 查询用户账户下的优惠券
     *
     * @param userResponse
     * @param orderPrice   订单总金额
     * @return
     */
    @GetMapping(value = "/coupon/findListByUser.json")
    public ResultMsg findListByUser(UserResponse userResponse, Integer orderPrice) {
        try {
            List<Coupon> list = couponService.findByUserId(userResponse.getUid(), orderPrice);
            return ResultMsg.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "查询失败");
        }
    }

    /**
     * 校验优惠券是否可用
     */
    @GetMapping(value = "/coupon/checkCouponEnable.json")
    public ResultMsg checkCouponEnable(UserResponse userResponse, Integer orderPrice, Long couponId) {
        try {
            return couponService.checkCouponEnable(userResponse.getUid(), orderPrice, couponId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "查询失败");
        }
    }
}
